package com.lindroy.androidplayer.ui.adapter

import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.*
import com.google.android.material.chip.Chip
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.logic.model.ArticleResp
import com.lindroy.androidplayer.logic.model.KnowledgeNavResp
import com.lindroy.androidplayer.ui.webview.ContentActivity

/**
 * @author Lin
 * @date 2021/4/29
 * @function
 */
class NavigationIndexAdapter :
    BaseQuickAdapter<KnowledgeNavResp, BaseViewHolder>(R.layout.knowledge_index_item) {

    override fun convert(holder: BaseViewHolder, item: KnowledgeNavResp) {
        holder.setText(R.id.tvKnowledgeTitle, item.name)
        val rvKnowledge = holder.getView<RecyclerView>(R.id.rvKnowledge)
        val layoutManager = FlexboxLayoutManager(holder.itemView.context).apply {
            flexWrap = FlexWrap.WRAP
            flexDirection = FlexDirection.ROW
            justifyContent = JustifyContent.FLEX_START
            alignItems = AlignItems.CENTER
        }
        rvKnowledge.layoutManager = layoutManager
        rvKnowledge.adapter= DetailAdapter(item.articles,item.name)
    }

    private class DetailAdapter(data:List<ArticleResp>, val title:String):
        BaseQuickAdapter<ArticleResp, BaseViewHolder>(R.layout.knowledge_detail_index){
        init{
            setList(data)
        }

        override fun convert(holder: BaseViewHolder, item: ArticleResp) {
            holder.getView<Chip>(R.id.chipName).apply {
                text = item.title
                setOnClickListener {
                    ContentActivity.start(context,item.id,item.link,item.title,item.collect)
                }
            }
        }

    }
}